const db = require('../db/index')
const path = require('path')

exports.cartonRankList = (req, res) => {
  const sql = [`select * from ag_cartonrank order by likenums DESC`, `select * from ag_cartonlike where user_id=?`]
  db.query(sql[0], (err0,results) => {
    if(err0) {
      return res.cc(err0)
    } else {
      db.query(sql[1], req.user.id, (err1,results2) => {
        if(err1) return res.cc(err1)
        if( results.length === 0) return res.cc('获取点赞列表失败!')
        res.send({
          status:0,
          message:'获取动漫排行榜成功!',
          statusdata: results2,
          data: results
        })
      })
    }  
  })
}

exports.gameRankList = (req, res) => {
  const sql = `select * from ag_gamerank order by likenums DESC`
  db.query(sql, (err, results) => {
    if(err) return res.cc(err)
    res.send({
      status:0,
      message:'获取游戏排行榜成功!',
      data: results
    })
  })
}

// 动漫榜点赞功能
exports.addCartonLike = (req, res) => {
  const addlikeinfo = {
    user_id: req.user.id,
    carton_id: req.body.id
  }
  const sql = [`update ag_cartonrank set likenums=likenums+1 where id=?`, `insert into ag_cartonlike set ?`]
  // const sqls = `insert into ag_like (user_id,article_id) values (uid,id)`
  db.query(sql[0],req.body.id, (err0, results) => {
    if(err0) {
      return res.cc(err0)
    } else {
      db.query(sql[1],addlikeinfo, (err1, results) => {
        if(err1) return res.cc(err1)
        if(results.affectedRows !== 1) return res.cc('点赞失败!')
        res.send({
          status: 0,
          message: '点赞成功!',
          data: results
        })
      })
    }
  })
}
// 动漫榜取消点赞功能
exports.cancelCartonLike = (req, res) => {
  const cancellikeinfo = req.user.id + req.body.id
  const sql = [`update ag_cartonrank set likenums=likenums-1 where id=?`,`delete from ag_cartonlike where carton_id=? `]
  db.query(sql[0], req.body.id, (err0, results) => {
    if(err0) {
      return res.cc(err0)
    } else {
      db.query(sql[1], req.body.id, (err1, results) => {
       if(err1) return res.cc(err1)
       if(results.affectedRows !== 1) return res.cc('取消点赞失败!')
       res.cc('取消点赞成功!', 0)
      })
    }
  })
}

// 游戏排行榜点赞功能
exports.addGameLike = (req, res) => {
  const addlikeinfo = {
    user_id: req.user.id,
    game_id: req.body.id
  }
  const sql = [`update ag_gamerank set likenums=likenums+1 where id=?`, `insert into ag_gamelike set ?`]
  // const sqls = `insert into ag_like (user_id,article_id) values (uid,id)`
  db.query(sql[0],req.body.id, (err0, results) => {
    if(err0) {
      return res.cc(err0)
    } else {
      db.query(sql[1],addlikeinfo, (err1, results) => {
        if(err1) return res.cc(err1)
        if(results.affectedRows !== 1) return res.cc('点赞失败!')
        res.send({
          status: 0,
          message: '点赞成功!',
          data: results
        })
      })
    }
  })
}
// 动漫榜取消点赞功能
exports.cancelGameLike = (req, res) => {
  const cancellikeinfo = req.user.id + req.body.id
  const sql = [`update ag_gamerank set likenums=likenums-1 where id=?`,`delete from ag_gamelike where game_id=? `]
  db.query(sql[0], req.body.id, (err0, results) => {
    if(err0) {
      return res.cc(err0)
    } else {
      db.query(sql[1], req.body.id, (err1, results) => {
       if(err1) return res.cc(err1)
       if(results.affectedRows !== 1) return res.cc('取消点赞失败!')
       res.cc('取消点赞成功!', 0)
      })
    }
  })
}

// 添加动漫榜单的作品
exports.addCartonInfo = (req, res) => {
  const cartoninfo = {
    ...req.body,
    poster: path.join(req.file.filename),
  }
  const sql = `insert into ag_cartonrank set ?`
  db.query(sql, cartoninfo, (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('发布新的动漫作品失败！')
    res.cc('发布新的动漫作品成功！', 0)
  })
}

// 添加游戏榜单的作品
exports.addGameInfo = (req, res) => {
  const cartoninfo = {
    ...req.body,
    poster: path.join(req.file.filename),
  }
  const sql = `insert into ag_gamerank set ?`
  db.query(sql, cartoninfo, (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('发布新的游戏作品失败！')
    res.cc('发布新的游戏作品成功！', 0)
  })
}

// 删除动漫排行榜信息
exports.delCartonInfo = (req, res) => {
  const sql = `delete from ag_cartonrank where id=?`
  db.query(sql, req.body.id, (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('删除动漫作品失败！')
    res.cc('删除动漫作品成功！', 0)
  })
}

// 删除游戏排行榜信息
exports.delGameInfo = (req, res) => {
  const sql = `delete from ag_gamerank where id=?`
  db.query(sql, req.body.id, (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('删除游戏作品失败！')
    res.cc('删除游戏作品成功！', 0)
  })
}